import elu.base.model as model
from sqlalchemy.sql import text


class DocerModel:
    def __init__(self):
        self.engine = model.engine
        self.meta = model.meta
        self.table = self.meta.tables['data_docer']

    def c(self, name, intro, version, modified, shelf_uuid):
        na = 1
        raw = "SELECT * FROM data_docer WHERE name=%r" % name
        inspect = self.engine.execute(raw)
        inspect_result = [dict(row) for row in inspect]

        if len(inspect_result) > 0:
            result = 'duplicate'
        else:
            raw_nd = "INSERT INTO data_docer (name, intro, version, modified, shelf_uuid, na) VALUES (%r, %r, %r, %r, %r, %r)" % (name, intro, version, modified, shelf_uuid, na)
            self.engine.execute(raw_nd)
            result = 'success'

        return result

    def u(self):
        pass

    def r(self):
        raw = "SELECT * FROM data_docer"
        result = self.engine.execute(raw)
        return [dict(row) for row in result]

    def d(self, shelf_uuid):
        raw = "UPDATE data_docer SET na='0' WHERE shelf=%r" % shelf_uuid
        result = self.engine.execute(raw)
        return result
